# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import pymysql
from douban.settings import mysql_host,mysql_port,mysql_user,mysql_passwd,mysql_db_name


class DoubanPipeline:
    def __init__(self):
        host = mysql_host
        port = mysql_port
        user = mysql_user
        passwd = mysql_passwd
        dbname = mysql_db_name
        self.mydb = pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=dbname, charset='utf8')
        self.cursor = self.mydb.cursor()

    def process_item(self, item, spider):
        data = dict(item)
        old_sql = "select serial_number from `top250` where serial_number=%s"
        self.cursor.execute(old_sql, data['serial_number'])
        old_serial_number = self.cursor.fetchone()
        if old_serial_number == None:
            sql = "insert into `top250` (serial_number, movie_name, introduce, star, evaluate, description) values (%s,%s,%s,%s,%s,%s)"
            self.cursor.execute(sql, (data['serial_number'], data['movie_name'], data['introduce'], data['star'], data['evaluate'], data['description']))
            self.mydb.commit()
        return item
